package com.example.myhotel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myhotel.common.BaseContext;
import com.example.myhotel.common.R;
import com.example.myhotel.dto.BillDto;
import com.example.myhotel.dto.BookDto;
import com.example.myhotel.entity.Bill;
import com.example.myhotel.entity.Book;
import com.example.myhotel.entity.Customers;
import com.example.myhotel.entity.Rooms;
import com.example.myhotel.service.BillService;
import com.example.myhotel.service.BookService;
import com.example.myhotel.service.CustomersService;
import com.example.myhotel.service.RoomsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("bill")
@Slf4j
public class BillController {
    @Autowired
    private BillService billService;
    @Autowired
    private CustomersService customersService;
    @Autowired
    private RoomsService roomsService;
    @Autowired
    private BookService bookService;
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String userId){
        //分页查询构造器
        Page<Bill> billPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Bill> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null,Bill::getCustomerId,userId);
        wrapper.orderByDesc(Bill::getCreateTime);
        billService.page(billPage,wrapper);
        return R.success(billPage);
    }
    @GetMapping("/user_page")
    public R<Page> userPage(int page,int pageSize){
        Page<Bill> billpage = new Page<>(page,pageSize);
        Page<BillDto> billDtoPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Bill> wrapper = new LambdaQueryWrapper<>();
        String userId = BaseContext.getCurrentId();

        wrapper.eq(userId != null,Bill::getCustomerId,userId);
        billService.page(billpage,wrapper);
        //拷贝对象
        BeanUtils.copyProperties(billpage,billDtoPage,"records");
        List<Bill> records = billpage.getRecords();
        List<BillDto> list = records.stream().map(item -> {
            BillDto billDto = new BillDto();
            //拷贝属性
            BeanUtils.copyProperties(item,billDto);
            String roomId = billDto.getRoomId();
            Rooms room = roomsService.getById(roomId);
            billDto.setAddress(room.getAddress());
            return billDto;
        }).collect(Collectors.toList());
        billDtoPage.setRecords(list);
        return R.success(billDtoPage);
    }

    @PostMapping("/save")
    public R<String> save(@RequestBody Bill bill){
        if (bill != null){
            Customers customers = customersService.getById(bill.getCustomerId());

            //修改预定信息
            Book book = new Book();
            book.setState(2);
            book.setId(bill.getBookId());
            bookService.updateById(book);

            //添加账单
            bill.setPhone(customers.getPhone());
            billService.save(bill);


            return R.success("成功写入账单");
        }

        return R.error("传输数据有误");
    }

    @DeleteMapping("/del")
    public R<String> delete(String id){

        billService.removeById(id);
        return R.success("成功删除");
    }


    //退房操作
    @PostMapping("/cel_room")
    public R<String> celRoom(@RequestBody Map map){
        String roomId = map.get("roomId").toString();
        String id = map.get("id").toString();
        //更改房间信息
        LambdaQueryWrapper<Rooms> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Rooms::getId,roomId);
        //查询房间信息获取房间价格和更该房间信息
        Rooms room = roomsService.getOne(wrapper);
        room.setCount(room.getCount()+1);
        roomsService.updateById(room);

        //更改账单数据
        billService.billUpdate(id,room.getPrices());

        return R.success("成功退房");
    }

}
